<?php

/**
 * @Author: king
 * @Date:   2019-05-04 19:20:35
 * @Last Modified by:   king
 * @Last Modified time: 2019-05-30 14:20:08
 */
namespace App\Models\Api;
use App\Exceptions\ApiException;
use App\Models\Api\Base;
use Cache;

class LanmeiUserBindAlipay extends Base
{
    protected $table = 'user_bind_alipay';

    /**
     * 解绑支付宝
     * @param int $uid
     * @return bool|int
     */
    public function delAli($uid = 0)
    {
        try {

            $res = $this->where('uid', $uid)->delete();
        } catch (\Exception $e) {
            throw new ApiException('解绑失败', config('code.error'));
        }

        return $res;

    }

    /**
     * 绑定或更新支付宝信息
     * @param array $data
     * @param array $id
     * @return array|bool|false|int
     */
    public function saveAli($data = [], $id = [])
    {
        //新增
        if (empty($id)) {

            //判断当前支付宝账号是否已经绑定
            $AliInfo = $this->getAliNumberInfo($data['uid']);
            if ($AliInfo) {
                throw new ApiException('当前用户已绑定支付宝', config('code.error'));
            }
            try {
                $data = $this->insert($data);
            } catch (\Exception $e) {
                throw new ApiException('支付宝绑定失败', config('code.error'));
            }
        } //编辑
        else {
            try {
                $data = $this->insert($data, $id);
            } catch (\Exception $e) {
                throw new ApiException('支付宝绑定失败', config('code.error'));
            }
        }

        return $data;
    }

    /**
     * 获取支付宝绑定信息
     * @param int $uid
     * @param string $alipay
     * @return array|bool|false|\PDOStatement|string|\think\Model
     */
    public function getAliNumberInfo($uid = 0, $alipay = '')
    {
        if ($alipay) {
            try {
                $aliNumberInfo = $this->where('alipay_account', $alipay)
                    ->select('id','truename','alipay_account')
                    ->orderBy('id', 'DESC')
                    ->first();

                $aliNumberInfo = json_decode(json_encode($aliNumberInfo, true), true);
            } catch (\Exception $e) {
               throw new ApiException('获取支付宝信息失败', config('code.error'));
            }
        } else {
            try {
                $aliNumberInfo = $this->where('uid', $uid)
                    ->select('id','truename','alipay_account')
                    ->orderBy('id', 'DESC')
                    ->first();
                $aliNumberInfo = json_decode(json_encode($aliNumberInfo, true), true);
            } catch (\Exception $e) {
                throw new ApiException('获取支付宝信息失败', config('code.error'));
            }
        }

//        if ($aliNumberInfo == null || empty($aliNumberInfo))
//        {
//            $result = [
//                'id'=>0,
//                'truename' => '',
//                'alipay_account' => ''
//            ];
//        } else
//        {
//            $result = [
//                'id'=>$aliNumberInfo['id'],
//                'truename' => $aliNumberInfo['truename'],
//                'alipay_account' => $aliNumberInfo['alipay_account']
//            ];
//        }

        return $aliNumberInfo;
    }
}
